import * as apiService from '@/services/api.js';

import store from '@/store';

/**
 * 获取下级会员列表
 */
const lowLevelListApi = async({
                                date,
                                registerDate,
                                username = undefined,
                                userNameTerm = undefined,
                                status,
                                order,
                                sort,
                                page = 1,
                                size = 10
}) => {
  const [result,data] = await apiService.post(`${consts.PROXY_PREV}/proxy/memberList`, {
    userNameTerm: userNameTerm, //精准查询
    userName: username, //模糊查询
    status,
    proxyId: store.getters.userId,
    order,
    sort,
    // stime: _.result(date, '[0]'),
    // etime: _.result(date, '[1]'),
    // regStartTime: _.result(registerDate, '[0]'),
    // regEndTime: _.result(registerDate, '[1]'),
    stime: new Date(_.result(date, '[0]')).valueOf() || undefined,
    etime: new Date(_.result(date, '[1]')).valueOf() || undefined,
    regStartTime: new Date(_.result(registerDate, '[0]')).valueOf() || undefined,
    regEndTime: new Date(_.result(registerDate, '[1]')).valueOf() || undefined,
    page,
    size
  });

  return [result,data];
};

/**
 * 获取下级代理列表
 */
const lowAgencyListApi = async({
                                 date,
                                 parentName,
                                 username,
                                 level,
                                 sort,
                                 order,
                                 page,
                                 size = 10
                              }) => {
  const [result,data] = await apiService.post(`${consts.PROXY_PREV}/proxy/proxyList`, {
    userName: username,
    proxyId: store.getters.userId,
    parentName,
    order,
    sort,
    level,
    // stime: _.result(date, '[0]'),
    // etime: _.result(date, '[1]'),
    stime: new Date(_.result(date, '[0]')).valueOf() || undefined,
    etime: new Date(_.result(date, '[1]')).valueOf()|| undefined,
    page,
    size
  });

  return [result,data];
};

/**
 * 下级数量
 */
const childCountApi = async() => {
  const [result,data] = await apiService.post(`${consts.PROXY_PREV}/proxy/childCount`, {
    proxyId: store.getters.userId,
  });

  return [result,data];
};

/**
 * 会员详情
 */
const memberDetailApi = async({
                                userId,
                                stime,
                                etime
                              }) => {
  const [result,data] = await apiService.post(`${consts.PROXY_PREV}/proxy/memberDetail`, {
    proxyId: store.getters.userId,
    userId,
    stime,
    etime
  });

  return [result,data];
};

/**
 * 代理详情
 */
const secondLevelApi = async({
                                userId
                              }) => {
  const [result,data] = await apiService.post('/gl/proxy/level/credit/secondlevel/info', {
    // proxyId: store.getters.userId,
    proxyId: userId,
  });

  return [result,data];
};

/**
 * 代理给会员上分信息查询
 */
const proxyCreditInfoApi = async() => {
  const [result,data] = await apiService.post('/gl/proxy/credit/info');

  return [result,data];
};

/**
 * 代理给会员上分提交
 */
const creditSubmitApi = async({targetUserId, amount}) => {
  const [result,data] = await apiService.post('/gl/proxy/credit/payout', {
    targetUserId, amount
  });

  return [result,data];
};

/**
 * 代理给代理上分信息查询
 */
const proxyWithdrawInfoApi = async() => {
  const [result,data] = await apiService.post('/gl/proxy/level/credit/level/info');

  return [result,data];
};

/**
 * 代理给代理上分提交
 */
const withdrawSubmitApi = async({targetUserId, amount}) => {
  const [result,data] = await apiService.post('/gl/proxy/level/credit/level/payout', {
    targetUserId, amount
  });

  return [result,data];
};

/**
 * 新建下级代理
 */
const addProxyApi = async({username, password}) => {
  const [result,data] = await apiService.post('/gl/proxy/add', {
    username, password
  });

  return [result,data];
};

/**
 * 查询自己创建的下级列表
 */
const proxyListApi = async({page, size, type = 0}) => {
  const [result,data] = await apiService.post('/gl/proxy/list/created', {
    page, size, type
  });

  return [result,data];
};

export {
  lowLevelListApi,
  memberDetailApi,
  secondLevelApi,
  lowAgencyListApi,
  proxyCreditInfoApi,
  creditSubmitApi,
  proxyWithdrawInfoApi,
  withdrawSubmitApi,
  addProxyApi,
  proxyListApi,
  childCountApi,
};
